/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.query;

import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Felipe
 */
public class QueryController {
    
    private QueryListener mQueryListener = null;
    
    public QueryController() {
        this(null);
    }
    
    public QueryController(QueryListener pQueryListener) {
        mQueryListener = pQueryListener;
    }
    
    public void setUpdateListener(QueryListener pQueryListener) {
        mQueryListener = pQueryListener;
    }  
    
    public void select(String pQuery) {
        List list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            list = (List<Object>) session.createQuery(pQuery).list();
            if (mQueryListener != null)
                mQueryListener.onQueryComplete(list);
            session.flush();
            session.close();            
        } catch (Exception e) {
            e.printStackTrace();
            if (mQueryListener != null)
                mQueryListener.onQueryError(e);
        }     
    }
    
    public interface QueryListener {
        
        public void onQueryComplete(List pList);
        
        public void onQueryError(Exception e);      
        
    };
    
}